﻿#include <iostream>
#include <fstream>
#include <string>
#include  "SmartCar2.h"
using namespace std;


int main() {
    const int numCars = 10;
    Car cars[numCars];
    Student students[numCars];

    for (int i = 0; i < numCars; i++) {
        cars[i].setID("cqusn" + to_string(i + 1));
        cars[i].chassis.setAttributes("dp" + to_string(i + 1), "SCOUT MINI", 451.0, 490.0, 115.0, 0.0, "四轮四驱", 10.0);

        for (int j = 0; j < 4; j++) {
            cars[i].tires[j].setAttributes((j % 2 == 0) ? "公路轮" : "麦克纳姆轮", 175.0);
        }

        cars[i].agx.setAttributes("AGX Xavier", 32.0, 512, 64, 32.0, 32.0);
        cars[i].camera.setAttributes("RealSense D435i", "D430", "1920x1080", 30, 87, 90);
        cars[i].lidar.setAttributes("RS-Helios-16p", 16, 100.0, 8.0);
        cars[i].gyroscope.setAttributes("CH110", "NXP");
        cars[i].display.setAttributes(11.6, "super");
        cars[i].battery.setAttributes("24V/15Ah", 24.0, 2.0);

        students[i].inputInfoFromConsole(); // 从命令行输入学生信息
        cars[i].assignToStudent(students[i]);
    }

    ofstream file("car_info.txt");
    if (file.is_open()) {
        for (int i = 0; i < numCars; i++) {
            cars[i].save(file);
            students[i].save(file);
            file << "-----------------------------------------" << endl;
        }
        file.close();
        cout << "信息已保存到文件 smartcar_info.txt" << endl;
    }
    else {
        cerr << "无法打开文件" << endl;
    }

    ifstream readFile("car_info.txt");
    if (readFile.is_open()) {
        int currentCar = 0;
        char choice = ' ';

        while (true) {
            if (choice == 'n' && currentCar < numCars - 1) {
                currentCar++;
            }
            else if (choice == 'p' && currentCar > 0) {
                currentCar--;
            }

            cars[currentCar].print();
            students[currentCar].print();

            if (currentCar == 0) {
                cout << "按n键显示下一辆小车信息，按q退出程序：";
            }
            else if (currentCar == numCars - 1) {
                cout << "按p键显示上一辆小车信息，按q退出程序：";
            }
            else {
                cout << "按n键显示下一辆小车信息，按p键显示上一辆小车信息，按q退出程序：";
            }

            cin >> choice;

            if (choice == 'q') {
                break;
            }
        }

        readFile.close();
    }
    else {
        cerr << "无法打开文件" << endl;
    }

    return 0;
}
